<template>
  <t-tabs v-model="tabValue">
    <t-tab-panel value="1" label="专利信息">
      <t-form ref="form" class="step-form" labelWidth="120px" :colon="true">
        <t-row align="stretch" :gutter="14" style="flex: 1;">
          <t-col :span="6">
            <t-form-item label="专利名称" name="name">
              {{ objData.name }}
            </t-form-item>
            <t-form-item label="专利类型" name="patentType">
              {{ objData.patentType | filterByDict(dict.type.patent_type) }}
            </t-form-item>
            <t-form-item label="阶段" name="phaseType">
              {{ objData.patentType | filterByDict(dict.type.patent_phase_type) }}
            </t-form-item>
            <t-form-item label="专利状态" name="patentState">
              {{ objData.patentState | filterByDict(dict.type.patent_state) }}
            </t-form-item>
            <t-form-item label="专利号" name="patentNum">
              {{ objData.patentNum }}
            </t-form-item>
            <t-form-item label="申请日" name="applyDate">
              {{ objData.applyDate }}
            </t-form-item>
            <t-form-item label="授权公告日" name="authorizationDate">
              {{ objData.authorizationDate }}
            </t-form-item>
            <t-form-item label="授权公告号" name="authorizationNum">
              {{ objData.authorizationNum }}
            </t-form-item>
            <t-form-item label="填报单位" name="declareDeptName">
              {{ objData.declareDeptName }}
            </t-form-item>
            <t-form-item label="关键词" name="keyWord">
              {{ objData.keyWord }}
            </t-form-item>
            <t-form-item label="备注" name="note">
              {{ objData.note }}
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
    </t-tab-panel>

    <t-tab-panel value="2" label="专利权人">
      <patent-company :objId="objId" aipType="1" ref="patent-company" v-model="objData.companyList"
        :editOpen="false"></patent-company>
    </t-tab-panel>
    <t-tab-panel value="3" label="专利发明人">
      <patent-member :objId="objId" v-model="objData.memberList" :editOpen="false"></patent-member>
    </t-tab-panel>
    <t-tab-panel value="4" label="年度社会、经济效益">
      <patent-benefit :objId="objId" ref="patent-benefit" v-model="objData.benefitList"
        :editOpen="false"></patent-benefit>
    </t-tab-panel>
    <t-tab-panel value="5" label="专利项目来源">
      <patent-project :objId="objId" ref="patent-project" v-model="objData.projectList"
        :editOpen="false"></patent-project>
    </t-tab-panel>
    <t-tab-panel value="6" label="附件信息">
      <t-table row-key="id" :columns="projectFileColumns" :data="projectFileData">
        <template #id="{ row, rowIndex }">
          {{ rowIndex + 1 }}
        </template>
        <template #c1="{ row }">
          <a v-if="row.c3" class="t-button-link" :href="`${file_domain}/${row.c3}`" target="_blank">{{ row.c1 }}</a>
          <div v-else>{{ row.c1 }}</div>
        </template>
        <template #op="{ row }">
          <div v-for="(item, index) in row.fileLists" :key="index">
            <a class="t-button-link" target="_blank" :href="item.url">
              {{ item.name }}
            </a>
          </div>
        </template>
      </t-table>
    </t-tab-panel>
  </t-tabs>
</template>

<script>
import { VITE_FILE_DOMAIN } from '@/api/upload'
import { getFiles } from "@/api/common";
import patentMember from "./patent-member.vue";
import patentCompany from "./patent-company.vue";
import patentProject from "./patent-project.vue";
import patentBenefit from "./patent-benefit.vue";

export default {
  name: "patent-detail",
  dicts: ['patent_type', 'patent_phase_type', 'patent_state'],
  components: {
    patentMember,
    patentCompany,
    patentProject,
    patentBenefit,
  },
  props: {
    objId: [String, Number],
  },
  data () {
    return {
      site_tag: import.meta.env.VITE_SITE_TAG,
      file_domain: VITE_FILE_DOMAIN,
      tabValue: '1',
      objData: {},
      projectFileData: [
        {
          id: 1,
          c1: "专利证书",
          c2: "是",
          moduleKey: "patentZS",
          fileIds: [],
          fileLists: [],
        },
        {
          id: 4,
          c1: "其他",
          c2: "否",
          fileIds: [],
          fileLists: [],
          moduleKey: "patentOther",
          require: false
        },
      ],
      projectFileColumns: [
        {
          colKey: "id",
          title: "序号",
          align: "center",
          width: 64
        },
        {
          colKey: "c1",
          title: "附件名称",
          align: "center",
        },
        {
          colKey: "c2",
          title: "是否必需",
          align: "center",
        },
        {
          colKey: "op",
          title: "附件列表",
          align: "left",
        },
      ],
      swiperFileList: []
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler (val) {
        if (val) {
          this.getObj(val);
        }
      }
    },
  },

  methods: {
    getObj () {
      // 获取项目附件
      getFiles(this.objId).then((res) => {
        const files = {}
        res.data.forEach((item) => {
          if (!files[item.moduleKey]) {
            files[item.moduleKey] = []
          }
          files[item.moduleKey].push({
            name: item.fileName,
            url: item.fullFilePath,
            id: item.id,
            filePath: item.filePath
          })
        })

        this.projectFileData.forEach((val, index) => {
          this.projectFileData[index].fileLists = files[val.moduleKey] || [];
          this.projectFileData[index].fileIds = files[val.moduleKey]?.map(val => val.id) || [];
        })

      }).catch((e) => {
        this.$message.error(e.toString());
      });
      this.$api.cgzh.patent.get(this.objId).then(response => {
        this.objData = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    }
  }
}
</script>

<style lang="less" scoped>
.t-tab-panel {
  padding: 10px;
}

.t-form__item {
  margin-bottom: 6px
}
</style>
